{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 函数参数\n",
    "* 最后一个形参为**name形式时，接收一个字典，该字典包含形参列表之外所有关键字参数\n",
    "* *name接收一个元组，该元组包含形参列表之外的所有位置参数\n",
    "* *name必须在**name之前"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def cheeseshop(kind, *args, **kwargs):\n",
    "    pass\n",
    "\n",
    "cheeseshop(\"pangyazhou\",\n",
    "           \"good\",\n",
    "           \"very\",\n",
    "           age=33,\n",
    "           address=\"hefei\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 特殊参数\n",
    "* 默认情况下，参数可以按位置或显式关键字传递给Python函数。\n",
    "* 可以限制参数传递方式\n",
    "函数定义如下\n",
    "```text\n",
    "def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):\n",
    "      -----------    ----------     ----------\n",
    "        |             |                  |\n",
    "        |        Positional or keyword   |\n",
    "        |                                - Keyword only\n",
    "         -- Positional only"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "def standard_arg(arg):\n",
    "    pass\n",
    "\n",
    "def pos_only_arg(arg, /):\n",
    "    pass\n",
    "\n",
    "def kwd_only_arg(*, arg):\n",
    "    pass\n",
    "\n",
    "def combined_example(pos_only, /, standard, *, kwd_only):\n",
    "    pass"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "standard_arg(2)\n",
    "standard_arg(arg=2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pos_only_arg() got some positional-only arguments passed as keyword arguments: 'arg'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[3], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m pos_only_arg(\u001B[38;5;241m2\u001B[39m)\n\u001B[1;32m----> 2\u001B[0m \u001B[43mpos_only_arg\u001B[49m\u001B[43m(\u001B[49m\u001B[43marg\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: pos_only_arg() got some positional-only arguments passed as keyword arguments: 'arg'"
     ]
    }
   ],
   "source": [
    "pos_only_arg(2)\n",
    "pos_only_arg(arg=2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "kwd_only_arg() takes 0 positional arguments but 1 was given",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[4], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m kwd_only_arg(arg\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m3\u001B[39m)\n\u001B[1;32m----> 2\u001B[0m \u001B[43mkwd_only_arg\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: kwd_only_arg() takes 0 positional arguments but 1 was given"
     ]
    }
   ],
   "source": [
    "kwd_only_arg(arg=3)\n",
    "kwd_only_arg(3)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "combined_example() takes 2 positional arguments but 3 were given",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[6], line 2\u001B[0m\n\u001B[0;32m      1\u001B[0m combined_example(\u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m2\u001B[39m, kwd_only\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m3\u001B[39m)\n\u001B[1;32m----> 2\u001B[0m \u001B[43mcombined_example\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m2\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m3\u001B[39;49m\u001B[43m)\u001B[49m\n",
      "\u001B[1;31mTypeError\u001B[0m: combined_example() takes 2 positional arguments but 3 were given"
     ]
    }
   ],
   "source": [
    "combined_example(1, 2, kwd_only=3)\n",
    "combined_example(1, 2, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4, 5]\n",
      "3 4 5\n"
     ]
    }
   ],
   "source": [
    "# 解包参数\n",
    "args = [3, 4, 5]\n",
    "print(args)\n",
    "print(*args)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'pangyazhou', 'arg': 33, 'address': 'hefei'}\n",
      "name arg address\n"
     ]
    }
   ],
   "source": [
    "kwargs = {}\n",
    "kwargs['name'] = \"pangyazhou\"\n",
    "kwargs['arg'] = 33\n",
    "kwargs['address'] = 'hefei'\n",
    "print(kwargs)\n",
    "print(*kwargs)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}